package CGIMan::File;

use strict;
use warnings;

use Data::Dumper;
use File::Spec;
use File::Basename;
use Moose;
with 'MooseX::Log::Log4perl';

#use MooseX::ClassAttribute;

=head1 NAME

 CGIMan::File

=head1 SYNOPSIS

  use CGIMan::File;
  my $file_obj = CGIMan::File->new(
                       full_path    => $file_path,
                       inc_dir_list => \@pod_dirs,
                       last_modified => $modified_date,
                       );

=head1 METHODS

=head2 inc_dir_list

This returns an array ref to the list of directories passed in which the file may reside.

=head2 full_path

Contains the full path to the file, from the root down.

=cut

has 'inc_dir_list' => (
    is  => 'rw',
    isa => 'ArrayRef[Str]',
);

=head2 full_path

=cut

has 'full_path' => (
    is  => 'rw',
    isa => 'Str',
);

=head2 inc_dir

Returns the value of the entry in @INC that is the parent of this
module's file.

=cut

has 'inc_dir' => ( is => 'rw', isa => 'Str' );

=head2 dirs

Returns the full path except for the file name.

=cut

has 'dirs' => (
    is       => 'rw',
    isa      => 'Str',
    lazy     => 0,
    init_arg => undef,    # do not allow in constructor
                          # builder  => 'parse_full_path',
);

=head2 file_name

Returns the file name, including the extension.

=cut

has 'file_name' => (
    is       => 'rw',
    isa      => 'Str',
    init_arg => undef,               # do not allow in constructor
    lazy     => 1,
    builder  => 'parse_full_path',
);

=head2 extension

Just returns the file etension.  Not even sure I am going to use this yet.

=cut

has 'extension' => (
    is       => 'rw',
    isa      => 'Str',
    init_arg => undef,    # do not allow in constructor
);

=head2 package_name

Returns the "::" delimited package name.

=cut

has 'package_name' => (
    is       => 'rw',
    isa      => 'Str',
    init_arg => undef,    # do not allow in constructor
);

=head2 namespace_path

This contains the directory paththat represents the namespace within
which the module (represented by the file) resides.

This corresponds to the keys in the %INC hash.

=cut

has 'namespace_path' => (
    is       => 'rw',
    isa      => 'Str',
    init_arg => undef,    # do not allow in constructor
);

has 'format' => (
    is  => 'rw',
    isa => 'Str',
);

=head2 inc_path

This contains the path in the directory that exists in @INC and in
which this file resides.

=cut

has 'inc_path' => (
    is       => 'rw',
    isa      => 'Str',
    init_arg => undef,    # do not allow in constructor
);

=head2 last_modified

Time of last modificiation

=cut

has 'last_modified' => (
    is  => 'rw',
    isa => 'DateTime',
);

=head2 version

$__PACKAGE__::VERSION

=cut

#has 'version' => (
#    is  => 'rw',
#    isa => 'Str',

#lazy => 1,
#builder => 'get_version',
#);

#sub get_version {
#    my $self = shift;
#    require $self->full_path;
#    return $self->namespace->VERSION();
#}

sub BUILD {
    my $self = shift;
    $self->parse_full_path;

    my $version;
    my $full_path = $self->full_path;

    #    eval { require $full_path; };
    #    if ($@) {
    #        warn "The problem is!: " . $@;
    #        $version = "no version";
    #    }
    #    else {
    #        eval { $version = $self->package_name->VERSION(); };
    #        $version = "no version" if $@;
    #    }
    #    $version ||= "no version";

    #warn "Version dump: " . Dumper($version);
    #$self->version($version);

    #return $self->namespace->VERSION();
}

sub parse_full_path {
    my $self = shift;
    my $log  = $self->log('parse_full_path');

    $log->trace( "Full path: " . $self->full_path );

    my ( $volume, $directories, $file_name ) =
      File::Spec->splitpath( $self->full_path );
    $log->fatal("DIR '$directories'");
    $self->dirs($directories);
    $self->file_name($file_name);

    # use dirs in inc_dir_list to generate regex to find which inc_dir
    # this file is in, and save it to inc_path
    $log->fatal( "G " . Dumper( $self->inc_dir_list ) );
    my $joined_paths = join '|', @{ $self->inc_dir_list };
    $log->trace("Joined path: '$joined_paths'");
    my $joined_paths_regex = qr|^($joined_paths)|;
    $log->debug( "jointed path regex: " . $joined_paths_regex );
    my ( $inc_path, $namespace_path ) =
      $self->full_path =~ m|$joined_paths_regex(.*)|;
    $log->trace("Inc path: '$inc_path'");
    $log->trace("Namespace path '$namespace_path'");

    $self->namespace_path($namespace_path);

    #warn "Namespace path: " . $self->namespace_path;

    $self->inc_path($inc_path);

    #warn "INC path: " . $self->inc_path;

    #Change slashes in directory to double colon-delimited
    ( my $package_name = $self->namespace_path ) =~ s|/|::|g;
    $package_name =~ s/^::(.*)\.pm$/$1/;
    $self->package_name($package_name) unless $self->package_name;

    # Set file name, extension
    my ( $name, $path, $suffix ) =
      fileparse( $self->full_path, qw( pod pm pl t ) );
    $self->file_name($name);
    $self->extension($suffix);

    #warn "Obj: " . Dumper($self);
}

no Moose;
__PACKAGE__->meta->make_immutable;

1;
